//
//  ScrollView.m
//  ZZScrollView
//
//  Created by xalo on 16/6/22.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "ScrollView.h"

#define kwidth self.frame.size.width
#define kheight self.frame.size.height

@interface ScrollView ()<UIScrollViewDelegate>
@property (nonatomic,strong)UIScrollView *scrollView;
//分页控制器
@property (nonatomic,strong)UIPageControl *pageControl;
//图片数组
@property (nonatomic,strong)NSArray *imageArray;
//当前下标
@property (nonatomic,assign)NSInteger index;
@property (nonatomic,strong)NSTimer *timer;
@end


@implementation ScrollView

- (instancetype)initWithFrame:(CGRect)frame imageArray:(NSArray *)imageArray
{
    self = [super initWithFrame:frame];
    if (self) {
        self.index = 1;
        self.imageArray = imageArray;
        [self createUI];

    }
    return self;
}
-(void)createUI{
    [self.scrollView addSubview:self.leftImageView];
    [self.scrollView addSubview:self.rightImageView];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.timer fire];
    });
//            [self.timer fire];
}
-(UIScrollView *)scrollView{
    if (!_scrollView) {
        //创建滚动视图及属性
        _scrollView = [[UIScrollView alloc]initWithFrame:self.frame];
        _scrollView.contentSize = CGSizeMake(kwidth * 2, kheight);
        _scrollView.bounces = NO;
        _scrollView.pagingEnabled = YES;
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.delegate = self;
        [self addSubview:_scrollView];

    }
    return _scrollView;
}
-(UIPageControl *)pageControl{
    if (!_pageControl) {
        //创建小圆点
        _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, kheight-50, 100, 50)];
        _pageControl.numberOfPages = self.imageArray.count;
        [self addSubview:_pageControl];

    }
    return _pageControl;
}
//创建两个图片

-(UIImageView *)leftImageView{
    if (!_leftImageView) {
        _leftImageView = [[UIImageView alloc]initWithFrame:self.frame];
        [_leftImageView  sd_setImageWithURL:[NSURL URLWithString:self.imageArray[0]]];
    }
    return _leftImageView;
}
-(UIImageView *)rightImageView{
    if (!_rightImageView) {
        _rightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(kwidth, 0, kwidth, kheight)];

//        _rightImageView.backgroundColor =[UIColor yellowColor];
        [_rightImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[self.index]]];
    }
    return _rightImageView;
}
-(NSTimer *)timer{
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    }
    return _timer;
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.x == kwidth) {
        scrollView.contentOffset = CGPointZero;
        
        self.pageControl.currentPage = self.index;
//        NSLog(@"ss%ld",self.pageControl.currentPage);
        self.index ++ ;
//        NSLog(@"53%ld",self.index);
        if (self.index == self.imageArray.count) {
            self.index = 0;
        }
        self.leftImageView.image = self.rightImageView.image;
        [_rightImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[self.index]]];

    }
  }

-(void)timerAction{
    [self.scrollView setContentOffset:CGPointMake(kwidth, 0) animated:YES];
}
@end
